<?php
/**
 *  
 * @package package_name
 * @author Mervin.Wong<mervin0502@163.com>
 * @Copyright
 * @license  http://www.apache.org/licenses/LICENSE-2.0
 * @version 0.1.0
 * @date 2013-2-24下午9:01:44
 *
 */
 
 /**
  * CanGoo User 控制器
  * 用户管理 控制器
  * @package  admin
  * @subpackage controllers
  * @category admin
  * 
  * 用户信息：用户名，用户昵称，邮箱，个人主页， 用户组
  * 用户列表：
  * 增加
  * 编辑
  * 删除
  * 
  */
  class User extends CI_Controller{
  	
  	/**
  	 * 构造函数
  	 */
  	public function __construct(){
  		parent::__construct();
  		
  		/* 验证*/
  		$this->load->helper('url');
  		$this->load->library('session');
  		$this->load->model('common_model');
  		if(!$this->common_model->is_login($this->session->userdata('session_id'))){
  			redirect(site_url('index/login', 'refresh'));
  		}
  		
  		
  		$this->load->helper('date');
  		
  		$this->load->model('user_model');
  		
  		$this->load->library('form_validation');
  		//验证规则
  		$this->form_validation->set_rules('user_name', '用户名', 'trim|required|min_length[5]|max_length[12]|alpha_dash|callback_check|xss_clean');//用户名
  		$this->form_validation->set_rules('user_slug', '用户昵称', 'trim|required|min_length[2]|max_length[12]|xss_clean');//用户昵称
  		$this->form_validation->set_rules('user_password','用户密码', 'trim|required|min_length[6]|matches[user_passconf]|md5');//密码
  		$this->form_validation->set_rules('user_passconf', '密码确认', 'trim|required|min_length[6]');//密码确认
  		$this->form_validation->set_rules('user_email', '电子邮件', 'trim|required|xss_clean|valid_email');//邮箱
  		$this->form_validation->set_rules('user_url', '个人主页', 'trim|xss_clean|prep_url');//网址
  		$this->form_validation->set_rules('user_description', '个人简介', 'trim|max_length[150]|xss_clean|prep_for_form');//个人简介
  	}
  	
  	/**
  	 * 用户管理首页，默认信息
  	 */
  	public function index(){
  		//用户列表
  		$data['user_list'] = $this->_table();
  		//用户表单
  		$action = 'user/add';
  		$head = '添加用户';
  		$values['name'] = '用户名';
  		$values['slug'] = '';
  		$values['email'] = '';
  		$values['url'] = 'mervin.me';
  		$values['group'] = 'visitor';
  		$values['description'] = '个人简介';
  		$values['uid'] = 0;
  		
  		$data['user_form'] = $this->_form($action, $head, $values);
  		$this->load->view('user_view', $data);
  	}
  	
  	/**
  	 * 添加一个用户
  	 * @access public
  	 * @return void description
  	 */
  	public function add(){
  		if($this->form_validation->run() == TRUE){
  			//通过验证，提交数据库
  			$this->_insert();
  			redirect(site_url('user/index'));
  		}else{
  			//验证失败
	   		//用户列表
	  		$data['user_list'] = $this->_table();
	  		//用户表单
	  		$action = 'user/add';
	  		$head = '添加用户';
	  		$values['name'] = set_value('user_name');
	  		$values['slug'] = set_value('user_slug');
	  		$values['email'] = set_value('user_email');
	  		$values['url'] = set_value('user_url');
	  		$values['group'] = $this->input->post('user_group');
	  		$values['description'] = set_value('user_description');
	  		$values['uid'] = 0;
	  		
	  		$data['user_form'] = $this->_form($action, $head, $values);
	  		$this->load->view('user_view', $data); 			
  		}
  	}
  	
  	/**
  	 * 编辑一个用户信息
  	 * @access public
  	 * @param int $uid description
  	 * @return void description
  	 */
  	public function edit($uid){
  		if($this->input->post('is_submit') != 'submit'){
  			//未提交
 	  		$data['user_list'] = $this->_table();
	  		//用户表单
	  		$action = 'user/edit/'.$uid;
	  		$head = '编辑用户';
	  		$user = $this->_get_one($uid);
	  		$values['name'] = $user['name'];
	  		$values['slug'] = $user['slug'];
	  		$values['email'] = $user['email'];
	  		$values['url'] = $user['url'];
	  		$values['group'] = $user['group'];
	  		$values['description'] = $user['description'];
	  		$values['uid'] = $user['uid'];
	  		
	  		$data['user_form'] = $this->_form($action, $head, $values);
	  		$this->load->view('user_view', $data); 	 			
  		}else{
	  		if($this->form_validation->run() == TRUE){
	  			//通过验证，提交数据库
	  			$this->_update($uid);
	  			redirect(site_url('user/index'));
	  		}else{
	  			//验证失败
		   		//用户列表
		  		$data['user_list'] = $this->_table();
		  		//用户表单
		  		$action = 'user/edit/'.$uid;
		  		$head = '编辑用户';
		  		$values['name'] = set_value('user_name');
		  		$values['slug'] = set_value('user_slug');
		  		$values['email'] = set_value('user_email');
		  		$values['url'] = set_value('user_url');
		  		$values['group'] = $this->input->post('user_group');
		  		$values['description'] = set_value('user_description');
		  		$values['uid'] = $uid;
		  		
		  		$data['user_form'] = $this->_form($action, $head, $values);
		  		$this->load->view('user_view', $data); 			
	  		}  			
  		}
  	}
  	
  	/**
  	 * 删除一个用户
  	 * @access public
  	 * @param int $uid description
  	 */
  	 public function del($uid){
  	 	$this->_delete($uid);
  	 	redirect(site_url('user/index'));
  	 }
  	
  	/**
  	 * 用户名重复性检测
  	 * @access public
  	 * @param string $value 表单域的值
  	 * @return bool description
  	 */
  	public function check($value){
  		if($this->user_model->check($value, 'name', $this->input->post('user_id'))){
  			$this->form_validation->set_messages('check', '%s:"'.$value.'"已存在！');
  		}else{
  			return true;
  		}
  	}
  	
  	/**
  	 * 获取一个用户的记录
  	 * @access public
  	 * @param int $uid description
  	 * @return array 一条用户记录
  	 */
  	private function _get_one($uid){
  		return $this->user_model->get_one($uid);
  	}
  	
  	/**
  	 * 插入一个用户
  	 */
  	private function _insert(){
  		//数据库字段 值
  		
  		$data['name'] = $this->input->post('user_name');
  		$data['slug'] = $this->input->post('user_slug');
  		$data['password'] = $this->input->post('user_password');
  		$data['email'] = $this->input->post('user_email');
  		$data['url'] = $this->input->post('user_url');
  		$data['group'] = $this->input->post('user_group');
  		$data['description'] = $this->input->post('user_description');
  		$data['regTime'] = now();
  		$data['modTime'] = $data['regTime'];
  		$data['lastTime'] = $data['regTime'];
  		$this->user_model->insert($data);
  	}
  	
  	private function _update($uid){
  		if($uid == $this->input->post('user_id')){
	   		$data['name'] = $this->input->post('user_name');
	  		$data['slug'] = $this->input->post('user_slug');
	  		$data['password'] = $this->input->post('user_password');
	  		$data['email'] = $this->input->post('user_email');
	  		$data['url'] = $this->input->post('user_url');
	  		$data['group'] = $this->input->post('user_group');
	  		$data['description'] = $this->input->post('user_description');
	  		$data['modTime'] = now();
	  		
	  		return $this->user_model->update($uid, $data); 		  			
  		}else{
  			return false;
  		}

  	}
  	
  	/**
  	 * 删除一条用户记录
  	 * @access public
  	 * @param int $uid description
  	 * @return bool description
  	 */
  	private function _delete($uid){
  		return $this->user_model->delete($uid);
  	}
  	/**
  	 * 用户列表 
  	 * @access private
  	 * @return array 表格信息
  	 */
  	 private function _table(){
  	 	$this->load->library('table');
  	 	//模板
 		$tmpl = array (
            'table_open'          => '<table id="user_table" border="0" cellpadding="4" cellspacing="0">',

            'heading_row_start'   => '<tr>',
            'heading_row_end'     => '</tr>',
            'heading_cell_start'  => '<th>',
            'heading_cell_end'    => '</th>',

            'row_start'           => '<tr class="t1">',
            'row_end'             => '</tr>',
            'cell_start'          => '<td>',
            'cell_end'            => '</td>',

            'row_alt_start'       => '<tr class="t2">',
            'row_alt_end'         => '</tr>',
            'cell_alt_start'      => '<td>',
            'cell_alt_end'        => '</td>',

            'table_close'         => '</table>'
        );
		$this->table->set_template($tmpl);
  	 	//标题栏
  	 	$this->table->set_heading('用户名','昵称','电子邮件','用户组', '文章' ,'操作');
  	 	$users = $this->user_model->get_users(array('uid', 'name', 'slug', 'email', 'group', 'description'));
  	 	$group = $this->config->item('group');
  	 	$table = array();
  	 	$i = 0;
  	 	foreach($users as $user){
  	 		$count = $this->user_model->get_post_count($user['uid']);
  	 		$table[$i]['name'] = $user['name'];
  	 		$table[$i]['slug'] = $user['slug'];
  	 		$table[$i]['email'] = $user['email'];
  	 		$table[$i]['group'] = $group[$user['group']];
  	 		$table[$i]['count'] = $count;
  	 		$table[$i]['operation'] = anchor(site_url('user/edit/'.$user['uid']), '编辑');
  	 		$table[$i]['operation'] .= '&nbsp;'.anchor(site_url('user/del/'.$user['uid']),'删除');
  	 		$i++;
  	 	}
  	 	return $this->table->generate($table);
  	 }
  	 
  	 /**
  	  * 用户信息表单
  	  * @access private
  	  * 
  	  */
  	  private function _form($action, $head, $values){
  	  	$this->load->helper('form');
  	  	$form = form_open($action, array('name'=>'user_form', 'id'=>'user_form'));
  	  	$form .= form_fieldset($head);
  	  	$form .= '<p>';
  	  	$form .= form_label('用户名:','user_name');
  	  	$form .= form_input(array('name'=>'user_name', 'id'=>'user_name', 'value'=>$values['name'], 'size'=>15));
  	  	$form .= '</p><p>';
  	  	$form .= form_label('用户昵称:','user_slug');
  	  	$form .= form_input(array('name'=>'user_slug', 'id'=>'user_slug', 'value'=>$values['slug'], 'size'=>15));
  	  	$form .= '</p><p>';
  	  	$form .= form_label('用户密码:', 'user_password');
  	  	$form .= form_password(array('name'=>'user_password', 'id'=>'user_password', 'size'=>15));
  	  	$form .= '</p><p>';
  	  	$form .= form_label('密码确认:','user_passconf');
  	  	$form .= form_password(array('name'=>'user_passconf', 'id'=>'user_passconf', 'size'=>15));
  	  	$form .= '</p><p>';
  	  	$form .= form_label('电子邮件:','user_email');
  	  	$form .= form_input(array('name'=>'user_email', 'id'=>'user_email', 'value'=>$values['email'], 'size'=>18));
  	  	$form .= '</p><p>';
  	  	$form .= form_label('个人主页:','user_url');
  	  	$form .= form_input(array('name'=>'user_url', 'id'=>'user_url', 'value'=>$values['url'], 'size'=>18));
  	  	$form .= '</p><p>';
  	  	$form .= form_label('用户组:', 'user_group');
  	  	$form .= form_dropdown('user_group', $this->config->item('group'), $values['group']);
  	  	$form .= '</p><p>';
  	  	$form .= form_label('个人简介', 'user_description');
  	  	$form .= form_textarea(array('name'=>'user_description', 'id'=>'user_description', 'rows'=>6, 'cols'=>25, 'value'=>$values['description']));
  	  	$form .= '</p><p>';
  	  	$form .= form_hidden('user_id', $values['uid']);
  	  	$form .= form_hidden('is_submit', 'submit');
  	  	$form .= form_reset('user_reset', '重写');
  	  	$form .= form_submit('user_submit', '提交');
  	  	$form .= '</p>';
  	  	$form .= form_fieldset_close();
  	  	$form .= form_close();
  	  	return $form;
  	  	
  	  	
  	  }
  }
  
  
/* End of file user.php */
/* Location : ./apps/admin/controllers/user.php */
